﻿using Sgr.BackGroundTasks;
using Sgr.Workspace.Services;

namespace Sgr.Workspace.BackGroundTasks
{
    /// <summary>
    /// 系统运营情况汇总后台任务
    /// </summary>
    public class AppOperationSummaryBackgroundTask : IBackGroundTask
    {
        private readonly IAppAnalysisService _appOperationSummaryService;
        private readonly ILogger<AppOperationSummaryBackgroundTask> _logger;

        public AppOperationSummaryBackgroundTask(
            IAppAnalysisService appOperationSummaryService,
            ILogger<AppOperationSummaryBackgroundTask> logger)
        {
            _appOperationSummaryService = appOperationSummaryService ?? throw new ArgumentNullException(nameof(appOperationSummaryService));
            _logger = logger ?? throw new ArgumentNullException(nameof(logger));
        }

        /// <summary>
        /// 执行审计日志全量周统计分析任务
        /// </summary>
        public async Task ExecuteAsync(CancellationToken cancellationToken = default)
        {
            _logger.LogInformation("AppOperationSummaryBackgroundTask 开始执行系统运营情况汇总后台任务，时间：{Time}", DateTime.UtcNow);

            try
            {
                await _appOperationSummaryService.AnalysisAsync(cancellationToken);
                _logger.LogInformation("AppOperationSummaryBackgroundTask 系统运营情况汇总后台任务执行完成。");
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "AppOperationSummaryBackgroundTask 执行过程中发生异常。");
                throw;
            }
        }
    }
}